home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevabuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.8 KB  |  394 lines

  1. /* Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevabuf.c,v 1.3 2000/09/19 19:00:11 lpd Exp $ */
  20. /* Alpha-buffering memory devices */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxdevice.h"
  25. #include "gxdevmem.h"        /* semi-public definitions */
  26. #include "gdevmem.h"        /* private definitions */
  27.  
  28. /* ================ Alpha devices ================ */
  29.  
  30. /*
  31.  * These devices store 2 or 4 bits of alpha.  They are a hybrid of a
  32.  * monobit device (for color mapping) and a 2- or 4-bit device (for painting).
  33.  * Currently, we only use them for character rasterizing, but they might be
  34.  * useful for other things someday.
  35.  */
  36.  
  37. /* We can't initialize the device descriptor statically very well, */
  38. /* so we patch up the image2 or image4 descriptor. */
  39. private dev_proc_map_rgb_color(mem_alpha_map_rgb_color);
  40. private dev_proc_map_color_rgb(mem_alpha_map_color_rgb);
  41. private dev_proc_map_rgb_alpha_color(mem_alpha_map_rgb_alpha_color);
  42. private dev_proc_copy_alpha(mem_alpha_copy_alpha);
  43.  
  44. void
  45. gs_make_mem_alpha_device(gx_device_memory * adev, gs_memory_t * mem,
  46.              gx_device * target, int alpha_bits)
  47. {
  48.     gs_make_mem_device(adev, gdev_mem_device_for_bits(alpha_bits),
  49.                mem, 0, target);
  50.     /* This is a black-and-white device ... */
  51.     adev->color_info = gdev_mem_device_for_bits(1)->color_info;
  52.     /* ... but it has multiple bits per pixel ... */
  53.     adev->color_info.depth = alpha_bits;
  54.     /* ... and different color mapping. */
  55.     set_dev_proc(adev, map_rgb_color, mem_alpha_map_rgb_color);
  56.     set_dev_proc(adev, map_color_rgb, mem_alpha_map_color_rgb);
  57.     set_dev_proc(adev, map_rgb_alpha_color, mem_alpha_map_rgb_alpha_color);
  58.     set_dev_proc(adev, copy_alpha, mem_alpha_copy_alpha);
  59. }
  60.  
  61. /* Reimplement color mapping. */
  62. private gx_color_index
  63. mem_alpha_map_rgb_color(gx_device * dev, gx_color_value r, gx_color_value g,
  64.             gx_color_value b)
  65. {
  66.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  67.     gx_color_index color = gx_forward_map_rgb_color(dev, r, g, b);
  68.  
  69.     return (color == 0 || color == gx_no_color_index ? color :
  70.         (gx_color_index) ((1 << mdev->log2_alpha_bits) - 1));
  71. }
  72. private int
  73. mem_alpha_map_color_rgb(gx_device * dev, gx_color_index color,
  74.             gx_color_value prgb[3])
  75. {
  76.     return
  77.     gx_forward_map_color_rgb(dev,
  78.                  (color == 0 ? color : (gx_color_index) 1),
  79.                  prgb);
  80. }
  81. private gx_color_index
  82. mem_alpha_map_rgb_alpha_color(gx_device * dev, gx_color_value r,
  83.            gx_color_value g, gx_color_value b, gx_color_value alpha)
  84. {
  85.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  86.     gx_color_index color = gx_forward_map_rgb_color(dev, r, g, b);
  87.  
  88.     return (color == 0 || color == gx_no_color_index ? color :
  89.         (gx_color_index) (alpha >> (gx_color_value_bits -
  90.                     mdev->log2_alpha_bits)));
  91. }
  92. /* Implement alpha copying. */
  93. private int
  94. mem_alpha_copy_alpha(gx_device * dev, const byte * data, int data_x,
  95.        int raster, gx_bitmap_id id, int x, int y, int width, int height,
  96.              gx_color_index color, int depth)
  97. {                /* Just use copy_color. */
  98.     return (color == 0 ?
  99.         (*dev_proc(dev, fill_rectangle)) (dev, x, y, width, height,
  100.                           color) :
  101.         (*dev_proc(dev, copy_color)) (dev, data, data_x, raster, id,
  102.                       x, y, width, height));
  103. }
  104.  
  105. /* ================ Alpha-buffer device ================ */
  106.  
  107. /*
  108.  * This device converts graphics sampled at a higher resolution to
  109.  * alpha values at a lower resolution.  It does this by accumulating
  110.  * the bits of a band and then converting the band to alphas.
  111.  * In order to make this work, the client of the device must promise
  112.  * only to visit each band at most once, except possibly for a single
  113.  * scan line overlapping the adjacent band, and must promise only to write
  114.  * a single color into the output.  In particular, this works
  115.  * within a single call on gx_fill_path (if the fill loop is constrained
  116.  * to process bands of limited height on each pass) or a single masked image
  117.  * scanned in Y order, but not across such calls and not for other
  118.  * kinds of painting operations.
  119.  *
  120.  * We implement this device as a subclass of a monobit memory device.
  121.  * (We put its state in the definition of gx_device_memory just because
  122.  * actual subclassing introduces a lot of needless boilerplate.)
  123.  * We only allocate enough bits for one band.  The height of the band
  124.  * must be a multiple of the Y scale factor; the minimum height
  125.  * of the band is twice the Y scale factor.
  126.  *
  127.  * The bits in storage are actually a sliding window on the true
  128.  * oversampled image.  To avoid having to copy the bits around when we
  129.  * move the window, we adjust the mapping between the client's Y values
  130.  * and our own, as follows:
  131.  *      Client          Stored
  132.  *      ------          ------
  133.  *      y0..y0+m-1      n-m..n-1
  134.  *      y0+m..y0+n-1    0..n-m-1
  135.  * where n and m are multiples of the Y scale factor and 0 <= m <= n <=
  136.  * the height of the band.  (In the device structure, m is called
  137.  * mapped_start and n is called mapped_height.)  This allows us to slide
  138.  * the window incrementally in either direction without copying any bits.
  139.  */
  140.  
  141. /* Procedures */
  142. private dev_proc_close_device(mem_abuf_close);
  143. private dev_proc_copy_mono(mem_abuf_copy_mono);
  144. private dev_proc_fill_rectangle(mem_abuf_fill_rectangle);
  145. private dev_proc_get_clipping_box(mem_abuf_get_clipping_box);
  146.  
  147. /* The device descriptor. */
  148. private const gx_device_memory mem_alpha_buffer_device =
  149. mem_device("image(alpha buffer)", 0, 1,
  150.        gx_forward_map_rgb_color, gx_forward_map_color_rgb,
  151.      mem_abuf_copy_mono, gx_default_copy_color, mem_abuf_fill_rectangle,
  152.        gx_no_strip_copy_rop);
  153.  
  154. /* Make an alpha-buffer memory device. */
  155. /* We use abuf instead of alpha_buffer because */
  156. /* gcc under VMS only retains 23 characters of procedure names. */
  157. void
  158. gs_make_mem_abuf_device(gx_device_memory * adev, gs_memory_t * mem,
  159.              gx_device * target, const gs_log2_scale_point * pscale,
  160.             int alpha_bits, int mapped_x)
  161. {
  162.     gs_make_mem_device(adev, &mem_alpha_buffer_device, mem, 0, target);
  163.     adev->max_fill_band = 1 << pscale->y;
  164.     adev->log2_scale = *pscale;
  165.     adev->log2_alpha_bits = alpha_bits >> 1;    /* works for 1,2,4 */
  166.     adev->mapped_x = mapped_x;
  167.     set_dev_proc(adev, close_device, mem_abuf_close);
  168.     set_dev_proc(adev, get_clipping_box, mem_abuf_get_clipping_box);
  169.     adev->color_info.anti_alias.text_bits =
  170.       adev->color_info.anti_alias.graphics_bits =
  171.     alpha_bits;
  172. }
  173.  
  174. /* Test whether a device is an alpha-buffering device. */
  175. bool
  176. gs_device_is_abuf(const gx_device * dev)
  177. {                /* We can't just compare the procs, or even an individual proc, */
  178.     /* because we might be tracing.  Instead, check the identity of */
  179.     /* the device name. */
  180.     return dev->dname == mem_alpha_buffer_device.dname;
  181. }
  182.  
  183. /* Internal routine to flush a block of the buffer. */
  184. /* A block is a group of scan lines whose initial Y is a multiple */
  185. /* of the Y scale and whose height is equal to the Y scale. */
  186. private int
  187. abuf_flush_block(gx_device_memory * adev, int y)
  188. {
  189.     gx_device *target = adev->target;
  190.     int block_height = 1 << adev->log2_scale.y;
  191.     int alpha_bits = 1 << adev->log2_alpha_bits;
  192.     int ddepth =
  193.     (adev->width >> adev->log2_scale.x) << adev->log2_alpha_bits;
  194.     uint draster = bitmap_raster(ddepth);
  195.     int buffer_y = y - adev->mapped_y + adev->mapped_start;
  196.     byte *bits;
  197.  
  198.     if (buffer_y >= adev->height)
  199.     buffer_y -= adev->height;
  200.     bits = scan_line_base(adev, buffer_y);
  201.     {                /*
  202.                  * Many bits are typically zero.  Save time by computing
  203.                  * an accurate X bounding box before compressing.
  204.                  * Unfortunately, in order to deal with alpha nibble swapping
  205.                  * (see gsbitops.c), we can't expand the box only to pixel
  206.                  * boundaries:
  207.                  int alpha_mask = -1 << adev->log2_alpha_bits;
  208.                  * Instead, we must expand it to byte boundaries,
  209.                  */
  210.     int alpha_mask = ~7;
  211.     gs_int_rect bbox;
  212.     int width;
  213.  
  214.     bits_bounding_box(bits, block_height, adev->raster, &bbox);
  215.     bbox.p.x &= alpha_mask;
  216.     bbox.q.x = (bbox.q.x + ~alpha_mask) & alpha_mask;
  217.     width = bbox.q.x - bbox.p.x;
  218.     bits_compress_scaled(bits, bbox.p.x, width, block_height,
  219.                  adev->raster, bits, draster, &adev->log2_scale,
  220.                  adev->log2_alpha_bits);
  221.     return (*dev_proc(target, copy_alpha)) (target,
  222.                       bits, 0, draster, gx_no_bitmap_id,
  223.                           (adev->mapped_x + bbox.p.x) >>
  224.                         adev->log2_scale.x,
  225.                         y >> adev->log2_scale.y,
  226.                          width >> adev->log2_scale.x, 1,
  227.                           adev->save_color, alpha_bits);
  228.     }
  229. }
  230. /* Flush the entire buffer. */
  231. private int
  232. abuf_flush(gx_device_memory * adev)
  233. {
  234.     int y, code = 0;
  235.     int block_height = 1 << adev->log2_scale.y;
  236.  
  237.     for (y = 0; y < adev->mapped_height; y += block_height)
  238.     if ((code = abuf_flush_block(adev, adev->mapped_y + y)) < 0)
  239.         return code;
  240.     adev->mapped_height = adev->mapped_start = 0;
  241.     return 0;
  242. }
  243.  
  244. /* Close the device, flushing the buffer. */
  245. private int
  246. mem_abuf_close(gx_device * dev)
  247. {
  248.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  249.     int code = abuf_flush(mdev);
  250.  
  251.     if (code < 0)
  252.     return code;
  253.     return mem_close(dev);
  254. }
  255.  
  256. /*
  257.  * Framework for mapping a requested imaging operation to the buffer.
  258.  * For now, we assume top-to-bottom transfers and use a very simple algorithm.
  259.  */
  260. typedef struct y_transfer_s {
  261.     int y_next;
  262.     int height_left;
  263.     int transfer_y;
  264.     int transfer_height;
  265. } y_transfer;
  266. private void
  267. y_transfer_init(y_transfer * pyt, gx_device * dev, int ty, int th)
  268. {
  269.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  270.     int bh = 1 << mdev->log2_scale.y;
  271.  
  272.     if (ty < mdev->mapped_y || ty > mdev->mapped_y + mdev->mapped_height) {
  273.     abuf_flush(mdev);
  274.     mdev->mapped_y = ty & -bh;
  275.     mdev->mapped_height = bh;
  276.     memset(scan_line_base(mdev, 0), 0, bh * mdev->raster);
  277.     }
  278.     pyt->y_next = ty;
  279.     pyt->height_left = th;
  280.     pyt->transfer_height = 0;
  281. }
  282. /* while ( yt.height_left > 0 ) { y_transfer_next(&yt, mdev); ... } */
  283. private void
  284. y_transfer_next(y_transfer * pyt, gx_device * dev)
  285. {
  286.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  287.     int my = mdev->mapped_y, mh = mdev->mapped_height;
  288.     int ms = mdev->mapped_start;
  289.     int ty = pyt->y_next += pyt->transfer_height;
  290.     int th = pyt->height_left;
  291.     int bh = 1 << mdev->log2_scale.y;
  292.  
  293.     /* From here on, we know that my <= ty <= my + mh. */
  294.     int tby, tbh;
  295.  
  296.     if (ty == my + mh) {    /* Add a new block at my1. */
  297.     if (mh == mdev->height) {
  298.         abuf_flush_block(mdev, my);
  299.         mdev->mapped_y = my += bh;
  300.         if ((mdev->mapped_start = ms += bh) == mh)
  301.         mdev->mapped_start = ms = 0;
  302.     } else {        /* Because we currently never extend backwards, */
  303.         /* we know we can't wrap around in this case. */
  304.         mdev->mapped_height = mh += bh;
  305.     }
  306.     memset(scan_line_base(mdev, (ms == 0 ? mh : ms) - bh),
  307.            0, bh * mdev->raster);
  308.     }
  309.     /* Now we know that my <= ty < my + mh. */
  310.     tby = ty - my + ms;
  311.     if (tby < mdev->height) {
  312.     tbh = mdev->height - ms;
  313.     if (tbh > mh)
  314.         tbh = mh;
  315.     tbh -= tby - ms;
  316.     } else {            /* wrap around */
  317.     tby -= mdev->height;
  318.     tbh = ms + mh - dev->height - tby;
  319.     }
  320.     if_debug7('V',
  321.           "[V]abuf: my=%d, mh=%d, ms=%d, ty=%d, th=%d, tby=%d, tbh=%d\n",
  322.           my, mh, ms, ty, th, tby, tbh);
  323.     if (tbh > th)
  324.     tbh = th;
  325.     pyt->height_left = th - tbh;
  326.     pyt->transfer_y = tby;
  327.     pyt->transfer_height = tbh;
  328. }
  329.  
  330. /* Copy a monobit image. */
  331. private int
  332. mem_abuf_copy_mono(gx_device * dev,
  333.            const byte * base, int sourcex, int sraster, gx_bitmap_id id,
  334.     int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  335. {
  336.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  337.     y_transfer yt;
  338.  
  339.     if (zero != gx_no_color_index || one == gx_no_color_index)
  340.     return_error(gs_error_undefinedresult);
  341.     x -= mdev->mapped_x;
  342.     fit_copy_xyw(dev, base, sourcex, sraster, id, x, y, w, h);    /* don't limit h */
  343.     if (w <= 0 || h <= 0)
  344.     return 0;
  345.     mdev->save_color = one;
  346.     y_transfer_init(&yt, dev, y, h);
  347.     while (yt.height_left > 0) {
  348.     y_transfer_next(&yt, dev);
  349.     (*dev_proc(&mem_mono_device, copy_mono)) (dev,
  350.                        base + (yt.y_next - y) * sraster,
  351.                       sourcex, sraster, gx_no_bitmap_id,
  352.                     x, yt.transfer_y, w, yt.transfer_height,
  353.                      gx_no_color_index, (gx_color_index) 1);
  354.     }
  355.     return 0;
  356. }
  357.  
  358. /* Fill a rectangle. */
  359. private int
  360. mem_abuf_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  361.             gx_color_index color)
  362. {
  363.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  364.     y_transfer yt;
  365.  
  366.     x -= mdev->mapped_x;
  367.     fit_fill_xy(dev, x, y, w, h);
  368.     fit_fill_w(dev, x, w);    /* don't limit h */
  369.     /* or check w <= 0, h <= 0 */
  370.     mdev->save_color = color;
  371.     y_transfer_init(&yt, dev, y, h);
  372.     while (yt.height_left > 0) {
  373.     y_transfer_next(&yt, dev);
  374.     (*dev_proc(&mem_mono_device, fill_rectangle)) (dev,
  375.                     x, yt.transfer_y, w, yt.transfer_height,
  376.                                (gx_color_index) 1);
  377.     }
  378.     return 0;
  379. }
  380.  
  381. /* Get the clipping box.  We must scale this up by the number of alpha bits. */
  382. private void
  383. mem_abuf_get_clipping_box(gx_device * dev, gs_fixed_rect * pbox)
  384. {
  385.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  386.     gx_device *tdev = mdev->target;
  387.  
  388.     (*dev_proc(tdev, get_clipping_box)) (tdev, pbox);
  389.     pbox->p.x <<= mdev->log2_scale.x;
  390.     pbox->p.y <<= mdev->log2_scale.y;
  391.     pbox->q.x <<= mdev->log2_scale.x;
  392.     pbox->q.y <<= mdev->log2_scale.y;
  393. }
  394.